/*
 * Progetto PPS2012 -seegwords
 *
 * Ettore Chimenti
 * Giuliano De Angelis
 * 
 * @02.2013
 * 
 */

#include<vector>
#include<iostream>
#include<fstream>
#include<stdio.h>
#include "MotoreDiRicerca.h"
#include "gtest/gtest.h"

using namespace std;

namespace {
  
  void makefile(  const string& cont, const string& file, vector<string>* docs ) {
    ofstream of(file.c_str());  
    if (!of.good())
      cerr << "errore nella creazione file di test \n";  
    of << cont;	 
    of.close();
    docs->push_back(file);
  }
  
  void makefile_default(vector<string>* docs ){
    makefile( "Lorem ipsum dolor sit amet \n",				"/tmp/doctest1.txt" , docs);	//PASSAGGIO FILE 
    makefile( "@@Lorem,,. !iPsUm@@ #doLoR!! #SIT; ##aMeT!! \n", 	"/tmp/doctest2.txt" , docs);	//PASSAGGIO FILE CON PUNTEGGIATURA
    makefile( "ciao ettore\n giuliano", 				"/tmp/doctest3.txt" , docs);	//PASSAGGIO FILE 
    makefile( "giuliano ettore", 					"/tmp/doctest4.txt" , docs);	//PASSAGGIO FILE 
  }
  
  void deletefiles( vector<string>& docs ) {
    for (unsigned int i = 0; i<docs.size(); i++)
      remove(docs[i].c_str());
  }
  
  class MotoreDiRicercaTest : public::testing::Test {
  protected:
    virtual void SetUp() {
      makefile_default(&docs);
      ASSERT_TRUE(test.Index(docs));
      results = NULL;
    }     
    virtual void TearDown() {
      
      if ( results != NULL){  
	cout << "Searched: " << query << "\n";
	cout << "Results :\n";
	for (unsigned int i = 0 ; i<results->size(); i++)
	  cout << "\t  " << (*results)[i] << "\n"; 
	delete results;
      }
      
      deletefiles(docs);
    }
    string query;
    vector<string> 	docs;
    vector<string> 	expect;
    vector<string>*	results;
    MotoreDiRicerca 	test;
  };
  
  TEST_F(MotoreDiRicercaTest, Index){
    //VERIFICA PASSAGGIO FILE
    ASSERT_TRUE(test.Index(docs));
  }
  
  TEST_F(MotoreDiRicercaTest, Search_Mono){
    // VERIFICA PAROLA SINGOLA
    query  = "ciao";
    results = test.Search(query);  
    
    expect.push_back(docs[2]);
    
    EXPECT_EQ(1,results->size());
    
    for (unsigned int i = 0 ; i<results->size(); i++)
      ASSERT_EQ(expect[i],(*results)[i]);
  }
  
  TEST_F(MotoreDiRicercaTest, Search_Mono_Punt){
    // VERIFICA PAROLA SINGOLA CON PUNTEGGIATURA
    query  = "dOLOr";
    results = test.Search(query);  
    
    expect.push_back(docs[0]);
    expect.push_back(docs[1]);

    ASSERT_EQ(2	,results->size());
    
    for (unsigned int i = 0 ; i<results->size(); i++)
      EXPECT_EQ(expect[i],(*results)[i]);  
  }
  
  TEST_F(MotoreDiRicercaTest, Search_Multi){
    query   = "\"ciao ettore giuliano\"";
    results = test.Search(query);
    
    expect.push_back(docs[2]);
    
    ASSERT_EQ(1,results->size());
    
    for (unsigned int i = 0 ; i<results->size(); i++)
      EXPECT_EQ(expect[i],(*results)[i]);
  }
  
  TEST_F(MotoreDiRicercaTest, Search_And){
    query = "ciao giuliano";
    results = test.Search(query);
    
    expect.push_back(docs[2]);
    ASSERT_EQ(1,results->size());
    
    for (unsigned int i = 0 ; i<results->size(); i++)
      EXPECT_EQ(expect[i],(*results)[i]);
  }
  
  TEST_F(MotoreDiRicercaTest, Search_Or){
    query = "ettore OR ciao";
    results = test.Search(query);
    
    expect.push_back(docs[2]);
    expect.push_back(docs[3]);
    
    EXPECT_EQ(2,results->size());
    
    for (unsigned int i = 0 ; i<results->size(); i++)
      EXPECT_EQ(expect[i],(*results)[i]);
  }

  TEST_F(MotoreDiRicercaTest, Search_Not){
    query = "          giuliano       ettore     -ciao    ";
    results = test.Search(query); //Gestione di spazi multipli
    expect.push_back(docs[3]);
    
    EXPECT_EQ(1,results->size());
    
    for (unsigned int i = 0 ; i<results->size(); i++)
      EXPECT_EQ(expect[i],(*results)[i]);
  }
  
  TEST_F(MotoreDiRicercaTest, Search_All){
    query = "         giuliano       ettore     -ciao  OR \"sit    amet\"  ";
    results = test.Search(query); //Gestione di spazi multipli
    expect.push_back(docs[0]);
    expect.push_back(docs[1]);
    expect.push_back(docs[3]);
    
    EXPECT_EQ(3,results->size());
    
    for (unsigned int i = 0 ; i<results->size(); i++)
      EXPECT_EQ(expect[i],(*results)[i]);
  }
  
}

int main(int argc, char **argv) {
  testing::InitGoogleTest(&argc, argv);
  
    cout <<"\n";
    cout <<"------------------ SEEGWORD GTESTING -------------------------\n";
    cout <<"----Search Engine by Ettore chimenti & Giuliano de angelis----\n";
    cout <<"--------------------------------------------------------------\n";
    cout <<"\n";
    cout << "Files usati:\n";
    cout <<"\t/tmp/doctest1.txt : "<< "Lorem ipsum dolor sit amet"				 << "\n";
    cout <<"\t/tmp/doctest2.txt : "<< "@@Lorem,,. !iPsUm@@ #doLoR!! #SIT; ##aMeT!!"	 	 << "\n";
    cout <<"\t/tmp/doctest3.txt : "<< "ciao ettore giuliano" 					 << "\n";
    cout <<"\t/tmp/doctest4.txt : "<< "giuliano ettore" 					 << "\n";
    cout <<"\n";
    
  return RUN_ALL_TESTS();
}
